import axios from 'axios';
import type { StandardQuestion } from './question';

export interface Point {
  id: number;
  keyword: string;
  value: number;
  active: boolean;
}

export interface StandardAnswer {
  questionId: number;
  version: string;
  answer: string;
  updateTime: string;  // ISO格式日期字符串
  keyPoints?: Point[];
}

export async function getPointsByAnswerId(
  questionId: number,
  version: string
): Promise<Point[]> {
  try {
    const response = await axios.get(`/api/points/${questionId}/${version}`);
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    console.log('获取关键点:', response.data);
    return response.data;
  } catch (error) {
    console.error('获取关键点失败:', error);
    throw error;
  }
}

export async function getStandardAnswersByQuestionId(
  id:number,
  version: string
): Promise<StandardAnswer> {
  try {
    const response = await axios.get(`/api/answers/standard/${id}/${version}`);
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    
    // 获取关键点
    if (response.data.questionId) {
      const points = await getPointsByAnswerId(response.data.questionId, version);
      return { ...response.data, keyPoints: points };
    }
    return response.data;
  } catch (error) {
    console.error('获取标准答案失败:', error);
    throw error;
  }
}

export async function updateStandardAnswer(
  id: number,
  version: string,
  answer: string
): Promise<void> {
  try {
    const response = await axios.put('/api/answers/standard', {
      id,
      version,
      answer
    });
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    return response.data;
  } catch (error) {
    console.error('更新标准答案失败:', error);
    throw error;
  }
}
export async function createStandardAnswer(
  answer: string,
  standardQuestionId: number,
  version: string
): Promise<void> {
  try {
    const response = await axios.post('/api/answers/standard', {
      answer,
      standardQuestionId,
      version,
      active: true
    });
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    return response.data;
  } catch (error) {
    if (axios.isAxiosError(error)) {
      console.error('创建标准答案失败 - 详细错误信息:', {
        message: error.message,
        code: error.code,
        status: error.response?.status,
        statusText: error.response?.statusText,
        requestUrl: error.config?.url,
        requestData: error.config?.data,
        responseData: error.response?.data
      });
    } else {
      console.error('创建标准答案失败 - 未知错误类型:', error);
    }
    throw error;
  }
}

interface LLMAnswerRequest {
  modelNames: string[]
  questionIds: number[]
  version: string
}

interface LLMAnswerResponse {
  question_id: string
  model_id: string
  answer: string
  timestamp: string
}

export async function generateLLMAnswer(
  request: LLMAnswerRequest
): Promise<LLMAnswerResponse[]> {
  try {
    const response = await axios.post('/api/llm/generate', request)
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    })
    return response.data
  } catch (error) {
    console.error('生成LLM回答失败:', error)
    throw error
  }
}

export interface LlmAnswer {
  id: number;
  model: string;
  answer: string;
  time: string;
  question: StandardQuestion;
}

export interface CandidateAnswer {
  id: number;
  standardQuestionId: number;
  version: string;
  answer: string;
  author: string;
  updateTime: string;  // ISO格式日期字符串
}

export async function getCandidateAnswersByQuestionId(
  questionId: number,
  version: string
): Promise<CandidateAnswer[]> {
  try {
    const response = await axios.get(`/api/candidate-answers/by-question/${questionId}/${version}`);
    return response.data;
  } catch (error) {
    console.error('获取候选回答失败:', error);
    throw error;
  }
}

export async function createCandidateAnswer(
  request: {standardQuestionId: number, version: string, answer: string, author: string}
): Promise<CandidateAnswer> {
  try {
    const response = await axios.post('/api/candidate-answers', request);
    return response.data;
  } catch (error) {
    console.error('创建候选回答失败:', error);
    throw error;
  }
}

export async function deleteCandidateAnswer(
  id: number,
  version: string
): Promise<void> {
  try {
    await axios.delete(`/api/candidate-answers/${id}/${version}`);
  } catch (error) {
    console.error('删除候选回答失败:', error);
    throw error;
  }
}

export async function getLlmAnswers(
  model?: string,
  questionId?: number
): Promise<LlmAnswer[]> {
  try {
    const params = new URLSearchParams();
    if (model) params.append('modelName', model);
    if (questionId) params.append('questionId', questionId.toString());    
    const response = await axios.get('/api/answers/llm', { params });
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data,
      fullUrl: `${response.config.url}?${params.toString()}`
    });
    return response.data;
  } catch (error) {
    console.error('获取LLM回答失败:', error);
    throw error;
  }
}